package cn.iocoder.yudao.module.hs.netty;

import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

/**
 * RFID标签蜂鸣指令
 */
@Slf4j
public class RFIDUtil {

    private static final String COMMAND_PREFIX = "#SET_TAG_DATA";

    public static final String RED = "110000";

    public static final String GREEN = "001100";

    public static final String BLUE = "000011";

    public static final String YELLOW = "111100";

    public static final String NO_LIGHT = "000000";

    private static final String FREQUENCY = "10_3_3";

    private static final String FREQUENCY_NO_BUZZING = "10_3_0";

    /**
     * @param rfid 标签编码
     * @param color 颜色 1-红色，2-蓝色，3-绿色，4-黄色
     * @param switchFlg 蜂鸣闪烁开关，true开启，false关闭
     * @param buzzingSeconds 蜂鸣闪烁时长，单位秒
     * @return
     */
    public static String getCommand(String rfid, Integer color, boolean switchFlg, Integer buzzingSeconds, boolean buzzingFlg) {
        StringBuilder sb = new StringBuilder(COMMAND_PREFIX);
        sb.append(" ").append("FF02").append("_").append(rfid).append(" ");
        if (switchFlg) {
            sb.append("T");
        } else {
            sb.append("0");
        }
        String seconds = String.format("%05d", buzzingSeconds);
        sb.append(seconds).append("_");
        if (color.equals(1)) {
            sb.append(RED);
        } else if (color.equals(2)) {
            sb.append(BLUE);
        } else if (color.equals(3)) {
            sb.append(GREEN);
        } else if (color.equals(4)) {
            sb.append(YELLOW);
        } else if (color.equals(0)) {
            sb.append(NO_LIGHT);
        }
        sb.append("_");
        if (buzzingFlg) {
            sb.append(FREQUENCY);
        } else {
            sb.append(FREQUENCY_NO_BUZZING);
        }
        return sb.toString();
    }

    /**
     * 发送开始盘点指令
     */
    public static void startStocktake(Channel channel) {
        String command = "#SET_UPLOAD_TYPE 7";
        log.info("#####发送开始盘点指令：天线 - " + channel.remoteAddress() + "指令信息 - " + command);
        channel.writeAndFlush(command);
    }

    /**
     * 发送停止盘点指令
     */
    public static void stopStocktake(Channel channel) {
        String command = "#SET_UPLOAD_TYPE 0";
        log.info("#####发送停止盘点指令：天线 - " + channel.remoteAddress() + "指令信息 - " + command);
        channel.writeAndFlush(command);
    }

}
